package org.example.consumer;

import org.example.consumer.dao.ProductMapper;
import org.example.consumer.entity.Product;
import org.example.consumer.entity.ProductRecord;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@RabbitListener(queues = "MyDirectQueue")
public class DirectReceiver {
    @Autowired
    ProductMapper productMapper;

    @RabbitHandler(isDefault = true)
    public void process(HashMap ReceiveMessage){
        Product product = productMapper.findByProductNo(String.valueOf(ReceiveMessage.get("productNo")));
        System.out.println(product);
        if(product!=null&&product.getTotal()>0){
            product.setTotal(product.getTotal()-1);
            int i = productMapper.updateById(product);
            productMapper.insert(new ProductRecord(product.getProductNo(),i));
        }
    }
}
